adlab = {
	url:{
		//url_insert = <?php echo STATIC_DOMAIN ?>
	},	
	init:function(){
		try
		{			
			this.initEvent();
			this.initCampaignEvent();
			this.initAdcaseEvent();
			this.initAdproductEvent();
		}
		catch (e)
		{
			console.log(e);
		}
	},
	initEvent:function(){
		var This = this;
		This.load_content = function(){			
			var page = $("#current_page").html();
			$.ajax({
				url: "/admin/adlab/ajaxaddoc/"+page,
				success:function(response){					
					$(".tb-list").html(response);
					This.update();
				}
			});
			
			$.ajax({
				url: "/admin/adlab/statistics_docs",
				type:"post",
				success: function(response){
					$("#ul_content").html(response);					
				}
			});
		}
		This.update = function(){
			$(".doc-status").change(function(){
				ajaxData = {};
				ajaxData['id'] = $(this).parent().siblings(".doc-id").html();
				ajaxData['status'] = $(this).val();			
				$.ajax({
					url: "/admin/adlab/change",
					type: 'post',				
					data: ajaxData,
					success:function(response){
						This.load_content();
						console.log(response);
					}
				});
			});
			$(".doc-delete").click(function(){
				ajaxData = {};
				ajaxData['id'] = $(this).parent().siblings(".doc-id").html();					
				$.ajax({
					url: "/admin/adlab/delete",
					type: 'post',				
					data: ajaxData,
					success:function(response){
						This.load_content();						
						console.log(response);
					}
				});
			});
		}
		This.update();		
//		$(".add-doc-trigger").click(function(){
//			$('html, body').animate({scrollTop:$(".new-doc").offset().top}, 'slow');
//			$(".add-doc").slideDown();
//		});
//		$(".cancel_add").click(function(){
//			$(".add-doc").slideUp();
//			$("[name='title']").val('');			
//		});
//		$(".add_button").click(function(){
//			ajaxData = {};
//			ajaxData['title'] = $("#doc_title").val();			
//			ajaxData['category'] = $("#doc_category").val();
//			ajaxData['type'] = 4;			
//			if($("#doc_status").is(':checked')){
//				ajaxData['status'] = 1;
//			}
//			else{
//				ajaxData['status'] = 0;
//			}
//			$.ajax({
//				url: "/admin/adlab/insert",
//				type: 'post',				
//				data: ajaxData,
//				success:function(response){
//					This.load_content();
//					$(".add-doc").slideUp();
//					console.log(response);
//				}
//			});
//		});		
	},
	initAdproductEvent:function(){
		var This = this;
		This.load_content_product = function(){
			$.ajax({
				url: "/admin/adlab/ajaxadproduct",
				success:function(response){					
					$(".tb-list").html(response);
					This.update_product();
				}
			});
			
			$.ajax({
				url: "/admin/adlab/statistics_products",
				type:"post",
				success: function(response){
					$("#ul_content").html(response);					
				}
			});
		}
		This.update_product = function(){
			$(".product-status").change(function(){
				ajaxData = {};
				ajaxData['id'] = $(this).parent().siblings(".product-id").html();
				ajaxData['status'] = $(this).val();			
				$.ajax({
					url: "/admin/adlab/change_product",
					type: 'post',				
					data: ajaxData,
					success:function(response){
						console.log(response);
						This.load_content_product();
					}
				});
			});
			$(".product-delete").click(function(){
				ajaxData = {};
				ajaxData['id'] = $(this).parent().siblings(".product-id").html();					
				$.ajax({
					url: "/admin/adlab/delete_product",
					type: 'post',				
					data: ajaxData,
					success:function(response){
						This.load_content_product();						
						console.log(response);
					}
				});
			});
		}
		This.update_product();		
	},
	initAdcaseEvent:function(){
		var This = this;
		This.load_content_case = function(){
			var page = $("#current_page").html();
			$.ajax({
				url: "/admin/adlab/ajaxadcase/"+page,
				success:function(response){					
					$(".tb-list").html(response);
					This.update_case();
				}
			});
			
			$.ajax({
				url: "/admin/adlab/statistics_cases",
				type:"post",
				success: function(response){
					$("#ul_content").html(response);					
				}
			});
		}
		This.update_case = function(){
			$(".case-status").change(function(){
				ajaxData = {};
				ajaxData['id'] = $(this).parent().siblings(".case-id").html();
				ajaxData['status'] = $(this).val();			
				$.ajax({
					url: "/admin/adlab/change",
					type: 'post',				
					data: ajaxData,
					success:function(response){
						console.log(response);
						This.load_content_case();
					}
				});
			});
			$(".case-delete").click(function(){
				ajaxData = {};
				ajaxData['id'] = $(this).parent().siblings(".case-id").html();					
				$.ajax({
					url: "/admin/adlab/delete",
					type: 'post',				
					data: ajaxData,
					success:function(response){
						This.load_content_case();						
						console.log(response);
					}
				});
			});
		}
		This.update_case();		
	},
	initCampaignEvent:function(){
		var This = this;
		This.load_content_category = function(){
			$.ajax({
				url: "/admin/adlab/ajaxcampaign",
				success:function(response){					
					$(".tb-list").html(response);
					This.update_category();
				}
			});
		}
		This.update_category = function(){
			$(".campaign-status").change(function(){
				ajaxData = {};
				ajaxData['id'] = $(this).parent().siblings(".campaign-id").html();
				ajaxData['status'] = $(this).val();			
				$.ajax({
					url: "/admin/adlab/change_category",
					type: 'post',				
					data: ajaxData,
					success:function(response){
						This.load_content_category();
						console.log(response);
					}
				});
			});
			$(".campaign-delete").click(function(){
				ajaxData = {};
				ajaxData['id'] = $(this).parent().siblings(".campaign-id").html();					
				$.ajax({
					url: "/admin/adlab/delete_category",
					type: 'post',				
					data: ajaxData,
					success:function(response){
						This.load_content_category();						
						console.log(response);
					}
				});
			});
			$(".campaign-update").click(function(){
				var id = $(this).parent().siblings(".campaign-id").html();
				var title = $(this).parent().siblings(".campaign-title").html();				
				var status = $(this).parent().siblings(".campaign-status-hidden").html();
				console.log(status);
				if(status == 1)
				{
					$('#campaign_status_update').attr('checked', true);
				}
				else
				{
					$('#campaign_status_update').attr('checked', false);
				}
				$("#campaign_id_update").val(id);	
				$("#campaign_title_update").val(title);			
				$(".update-campaign").slideDown();						
			});
		}
		This.update_category();
		$(".add-campaign-trigger").click(function(){
			$('html, body').animate({scrollTop:$(".new-campaign").offset().top}, 'slow');
			$(".add-campaign").slideDown();
		});
		$(".cancel_add").click(function(){
			$(".add-campaign").slideUp();
			$("[name='title']").val('');			
		});
		$(".cancel_update").click(function(){
			$(".update-campaign").slideUp();					
		});
		$(".add_button_campaign").click(function(){
			ajaxData = {};
			ajaxData['title'] = $("#campaign_title").val();						
			ajaxData['type'] = 2;			
			if($("#campaign_status").is(':checked')){
				ajaxData['status'] = 1;
			}
			else{
				ajaxData['status'] = 0;
			}
			$.ajax({
				url: "/admin/adlab/insert_category",
				type: 'post',				
				data: ajaxData,
				success:function(response){
					This.load_content_category();
					$(".add-campaign").slideUp();
					console.log(response);
				}
			});
		});
		
		$(".update_button_campaign").click(function(){
			ajaxData = {};
			ajaxData['id'] = $("#campaign_id_update").val();
			ajaxData['title'] = $("#campaign_title_update").val();											
			if($("#campaign_status_update").is(':checked')){
				ajaxData['status'] = 1;
			}
			else{
				ajaxData['status'] = 0;
			}
			console.log(ajaxData);
			$.ajax({
				url: "/admin/adlab/update_category",
				type: 'post',				
				data: ajaxData,
				success:function(response){
					This.load_content_category();
					$(".update-campaign").slideUp();
					console.log(response);
				}
			});
		});		
		
	},
} 

$(document).ready(function(){	
	adlab.init();		               
});